<?php

namespace app\store\controller;

use think\Db;

class Zqatodb extends \think\Controller
{
    private $wxappid = 10015;
    private $daIdArr = array('A'=>1,'B'=>2,'C'=>3,'D'=>4);

    public function todb(){
        $paramArr = $this->request->param();
        $csvname = !empty($paramArr['csvname']) ? $paramArr['csvname'] : '';
        $isdb = !empty($paramArr['isdb']) ? $paramArr['isdb'] : 'no';
        $isrep = !empty($paramArr['isrep']) ? $paramArr['isrep'] : 'yes';
        if($csvname==""){
            echo '没有传递csvname';
        }
        else{
            $fileName = $_SERVER['DOCUMENT_ROOT'].'/myscript/qafiles/'.$csvname.'.CSV';
            $testArr = $this->read_csv($fileName);
//        echo '<pre>';
//        print_r($testArr);

            $newArr = $this->opmOldArr($testArr);
            if($isdb=='no'){
                echo '<pre>';
                print_r($newArr);
            }

            if($isdb=='yes') {
                $this->begInDb($newArr, $isrep);
            }
        }
    }

    private function begInDb($newArr, $isrep){
        $nowQaBoxTitle = "aabbcc";
        $boxId = 0;
        foreach ($newArr as $key => $value){
            $boxTitle = $value['boxTitle'];
            $perWenTi = $value['perWenTi'];
            $daan = $value['daan'];
            $xxArr = $value['xxArr'];

            // 新的试卷入库
            if($nowQaBoxTitle!=$boxTitle && $boxTitle!=''){
                $bq_id =  $this->getDataID('qa_boxquestion', ['bq_title'=>$boxTitle], 'bq_id');
                if($bq_id==-1){
                    $inArr11['bq_type1'] = $value['bq_type1'];   //1级分类id
                    $inArr11['bq_type2'] = $value['bq_type2'];   //2级分类id
                    $inArr11['bq_title'] = $boxTitle;
                    $inArr11['bq_des'] = '';
                    $inArr11['bq_easy'] = $value['bq_easy'];    //难易程度
                    $inArr11['bq_num'] = $value['qa_num']; //试卷个数
                    $inArr11['bq_price'] = $value['bq_price'];   //试卷价格
                    $inArr11['wxapp_id'] = $this->wxappid;
                    $inArr11['create_time'] = date('Y-m-d H:i:s',time());
                    $inArr11['update_time'] = date('Y-m-d H:i:s',time());
                    $boxId = Db::name('qa_boxquestion')->insertGetId($inArr11);
                    //print_r($inArr11);
                    echo '<br>++++++++++++++++++++++：'.$boxId.' ++++++++++++++++++++++';
                }
                else{
                    $boxId = $bq_id;
                }
            }
            // 循环每个试题入库
            if($boxId>0){
                $pq_id = $isrep=='no' ? $this->getDataID('qa_perquestion', ['pq_question'=>$perWenTi], 'pq_id') : -1;
                if($pq_id==-1){
                    $inArr22['pq_pid'] = $boxId;
                    $inArr22['pq_type1'] = $value['bq_type1'];
                    $inArr22['pq_type2'] = $value['bq_type2'];
                    $inArr22['pq_easy'] = $value['bq_easy'];
                    $inArr22['pq_question'] = $perWenTi;
                    $inArr22['pq_type'] = 1;    // 单选多选填空题
                    $inArr22['pq_ask1'] = $value['xxArr'][0];
                    $inArr22['pq_ask2'] = $value['xxArr'][1];
                    $inArr22['pq_ask3'] = $value['xxArr'][2];
                    $inArr22['pq_ask4'] = $value['xxArr'][3];
                    $inArr22['pq_right'] = $value['daan'];
                    $inArr22['wxapp_id'] = $this->wxappid;
                    $inArr22['create_time'] = date('Y-m-d H:i:s',time());
                    $inArr22['update_time'] = date('Y-m-d H:i:s',time());
                    $perAskId = Db::name('qa_perquestion')->insertGetId($inArr22);
                    echo '<br>------'.$boxId.'------'.$perAskId;
                }
                else{
                    echo '<br>------'.$boxId.'------题目重复了！！！';
                }
            }
        }
    }

    private function getDataID($sortName, $where, $backFiled){
        $res = Db::name($sortName)->where($where)->find();
        if($res){
            return $res[$backFiled];
        }
        else{
            return -1;
        }
    }

    private function opmOldArr($testArr){
        $newArr11 = array();
        foreach ($testArr as $key => $value){
            $smallArr = array();
            $qaTitle = !empty(trim($value[0])) ? trim($value[0]) : "";
            $smallArr['boxTitle'] = $qaTitle;
            $smallArr['bq_type1'] = trim($value[2]);
            $smallArr['bq_type2'] = trim($value[3]);
            $smallArr['bq_easy'] = trim($value[4]);
            $smallArr['bq_price'] = trim($value[5]);
            $smallArr['qa_num'] = trim($value[6]);

            $perQaStrOld = trim($value[1]);

            $perQaStrOld = $this->zeTiHuan("/题目\d+:/", "", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/^\d+. /", "", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/A. /", "###", $perQaStrOld, 1);

            $perQaStrOld = $this->zeTiHuan("/正确答案：/", "###", $perQaStrOld, 1);

            $perQaStrOld = $this->zeTiHuan("/答案：A. .*$/", "###A", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/答案：B. .*$/", "###B", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/答案：C. .*$/", "###C", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/答案：D. .*$/", "###D", $perQaStrOld, 1);

            $perQaStrOld = $this->zeTiHuan("/答案：A/", "###A", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/答案：B/", "###B", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/答案：C/", "###C", $perQaStrOld, 1);
            $perQaStrOld = $this->zeTiHuan("/答案：D/", "###D", $perQaStrOld, 1);


            $perQaStrOld = $this->zeTiHuan("/[B-D]\. /", "@@", $perQaStrOld);
            //echo $perQaStrOld.'<br>';   //一定要等到所有题目都录入了再批处理，删除无用的数据行，否则坑死你
            $perQaArr = explode('###', $perQaStrOld);
            //print_r($perQaArr); echo '<br><br>'; //调试打开此处，最正确的格式：   英语中有多少个英文字母？ ###24 @@26 @@28 @@30###B

            $smallArr['perWenTi'] = trim($perQaArr[0]);
            $smallArr['daan'] = $this->daIdArr[trim($perQaArr[2])];
            $smallArr['xxArr'] = explode('@@', trim($perQaArr[1]));
            $newArr11[] = $smallArr;
        }
        return $newArr11;
    }














    private function zeTiHuan($zzz, $str, $oldStr, $num=0){
        $backStr = "";
        if($num==0){
            $newStr = preg_replace($zzz, $str, $oldStr);
        }
        else{
            $newStr = preg_replace($zzz, $str, $oldStr, 1);
        }
        return $newStr;
    }
    private function read_csv($file)
    {
        $data = null;
        if(!is_file($file)&&!file_exists($file))
        {
            die('读取失败');
        }
        $cvs_file = fopen($file,'r');
        $i = 0;
        while ($file_data = fgetcsv($cvs_file))
        {
            $i++;
            if($i==1)
            {
                //continue;
            }
            //if($file_data[0]!='')
            //{
            //iconv('gbk','utf-8',var_export($row,true)).';');
            //$data[$i] = $file_data;
            foreach($file_data as $key => $val){
                $file_data[$key] = iconv('gbk','utf-8', $val.'');
            }
            $data[$i] = $file_data;
            // $data[$i] = iconv('gbk','utf-8', $file_data.'');
            //}

        }
        fclose($cvs_file);
        return $data;
    }
}
